<?php

namespace Admin\Controller;

class DogController extends AdminController {

    public function index() {

        //省
        $sheng = I("sheng", -1);
        if ($sheng != -1) {

            $where['province'] = M('Area')->where(array('id' => $sheng))->getField('name');
            $this->assign("sheng", $sheng);
        }

        //市
        $shi = I("shi", -1);
        if ($shi != -1) {
            $where['city'] = M('Area')->where(array('id' => $shi))->getField('name');

            $shi_list = M('Area')->where(array('fid' => $sheng))->select();
            $this->assign('shi_list', $shi_list);
            $this->assign("shi", $shi);
        }

        //区
        $qu = I("qu", -1);
        if ($qu != -1) {

            $where['district'] = M('Area')->where(array('id' => $qu))->getField('name');

            $qu_list = M('Area')->where(array('fid' => $shi))->select();
            $this->assign('qu_list', $qu_list);
            $this->assign("qu", $qu);
        }


        //状态
        $status = I("status", -1);
        if ($status != -1) {
            $where['d.status'] = $status;
            $this->assign("status", $status);
        }

        //是否绝育
        $is_ster = I("is_ster", -1);
        if ($is_ster != -1) {
            $where['is_ster'] = $is_ster;
            $this->assign("is_ster", $is_ster);
        }

        $keywords = I('keywords');
        if ($keywords) {

            $map['dog_name'] = array('like', '%' . $keywords . '%');
            $map['card_number'] = $keywords;
            $map['u.nick_name'] = array('like', '%' . $keywords . '%');

            $map['_logic'] = 'or';
            $where['_complex'] = $map;

            $this->assign('keywords', $keywords);
        }



        $field = "d.*,u.nick_name";
        $order = "d.create_time desc";

        $M = M('Dog')
                ->table('ot_dog d')
                ->join('ot_users u on d.user_id = u.user_id');
        $list = $this->lists($M, $where, $order, $field);

        if ($list) {
            foreach ($list as &$val) {
                $val['yuanbao'] = M('Reward')->where(['dog_id' => $val['dog_id']])->sum('number');

                if (empty($val['yuanbao'])) {
                    $val['yuanbao'] = 0;
                }
            }

            unset($val);
        }

        $sheng_list = M('Area')->where(array('fid' => 0))->select();
        $this->assign('sheng_list', $sheng_list);
        $this->assign('_list', $list);
        $this->assign('meta_title', '狗卡管理');
        $this->display();
    }

    public function info() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $info = M('Dog')->where(['dog_id' => $id])->find();

        if (empty($info)) {
            $this->error('找不到狗卡数据');
        } else {
            $info['yuanbao'] = M('Reward')->where(['dog_id' => $info['dog_id']])->sum('number');
            $info['nick_name'] = M('Users')->where(['user_id' => $info['user_id']])->getField('nick_name');
        }
        

        $this->assign('data', $info);
        $this->display();
    }

    public function getInfo() {
        $fid = I('fid', 0, 'intval');

        $list = M('Area')->where(array('fid' => $fid))->select();
        $this->ajaxReturn($list);
    }

    public function changeStatus() {
        $s = I('s', 0, 'intval');

        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $data['status'] = $s;

        $res = M('Dog')->where(['dog_id' => $id])->save($data);

        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

}
